package com.freshcity.page.friend
{
	import com.freshcity.component.alert.Alert;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.FilterConst;
	import com.freshcity.core.OpCodes;
	import com.freshcity.friend.BasicWin;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.page.talk.RequestTalk;
	import com.freshcity.utils.MessageUtil;
	import com.freshcity.utils.StringUtil;
	import com.greensock.TweenLite;
	
	import fl.events.ListEvent;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	
	public class BasicPanel extends Component
	{
		public var home:FriendHome;
		private var back:BasicWin;
		
		public function BasicPanel(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(parent, xpos, ypos);
			this.setSize(500,300);
			back.wordsBtn.addEventListener(MouseEvent.CLICK,onWordsBtnClickHandler);
			back.sayHelloBtn.addEventListener(MouseEvent.CLICK, onSayHelloBtnHandler);
			back.chatBtn.addEventListener(MouseEvent.CLICK,onReqChat);
			back.removeBtn.addEventListener(TextEvent.LINK, onRemoveAssoBtnHandler);
			back.availUpgrade.addEventListener(MouseEvent.CLICK,availUpgradeClickHandler);
			back.waterBtn.addEventListener(MouseEvent.CLICK,onWaterBtnHandler);
		}
		
		protected function onWaterBtnHandler(event:MouseEvent):void
		{
			if(main.user.sex=='female'){
				main.commu.send(187, {to: home.asso.id}, onReqWaterCallBack);
			}else{
				home.back.tabMenu.arrBtns[4].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		private function onReqWaterCallBack(data:*):void
		{
			if(data==true){
				main.msg.showRollTipsMsg("信息已发出");
				back.waterBtn.enabled=false;
			}else{
				var htmlContent:String="没有发现你的梧桐树哦。栽下梧桐树，找到真心人，你要立刻去栽树吗？";
				Alert.show(htmlContent, "", Alert.OK | Alert.CANCEL, null, forwardNewTree);
			}
		}
		
		private function forwardNewTree(arg1:int):void
		{
			if (arg1 == Alert.OK)
			{
				home.visible=false;
				main.mainNav.onShowTree();
			}
		}		
		
		
		protected function availUpgradeClickHandler(event:MouseEvent):void
		{
			if(home.asso.assoValue>=home.asso.upgradeExp){
				var toStr:String = home.asso.id;
				var rf:RequestFriend = new RequestFriend(toStr);
				rf.main = this.main;
				rf.show();
				//main.commu.send(OpCodes.UPGREADE_ASSO,{to:toStr},onUpgradeCallback);
			}else{
				main.msg.showRollTipsMsg("友好度不够，留言、问候、馈赠清水、交换礼物等都能提升友好度");
			}
		}
		
		private function onUpgradeCallback(data:*):void
		{
			home.asso = data;
			this.initlize();
		}
		
		protected function onRemoveAssoBtnHandler(event:Event):void
		{
			//var timeDesc:String = "".concat("<font color=\'#ff0000\' size=\'13\'>",back.dateCombox.selectedLabel,"</font>");
			var htmlContent:String="  情谊来之不易，解除关系将自动消除往来记录，确认继续吗？";
			var htmlTitle:String="<font color=\'#f5ff00\'>解除关系</font>";
			
			var _confirm:Function=function(arg1:int):void
			{
				if (arg1 == Alert.OK)
				{
					main.commu.send(OpCodes.CANCEL_GUANZHU, {to: String(home.asso.id)}, onRemoveAssoCallBack);
				}
			};
			Alert.show(htmlContent, htmlTitle, Alert.OK | Alert.CANCEL, null, _confirm);
		}
		
		private function onRemoveAssoCallBack(data:*):void
		{
			if (data == true)
			{
				main.msg.showRollTipsMsg("成功完成操作");
				home.hide();
				main.friendPanel.removeFriend(home.asso);
			}
			else
			{
				main.msg.showRollTipsMsg(data);
			}
		}
		
		protected function onReqChat(event:MouseEvent):void
		{
			new RequestTalk(this,home.asso.id);
		}
		
		protected function onSayHelloBtnHandler(event:MouseEvent):void
		{
			if (home.asso.canHello != null)
			{
				main.commu.send(OpCodes.SAY_HELLO, {to: String(home.asso.id)}, onSayHelloCallBack);
			}
			else
			{
				main.msg.showRollTipsMsg("今日招呼已经打过");
			}
		}
		
		private function onSayHelloCallBack(data:*):void
		{
			if (data == true)
			{
				main.msg.showRollTipsMsg("信息已发出");
				home.asso.canHello='false';
				TweenLite.delayedCall(5,initlize,null,true);
			}
			else
			{
				main.msg.showRollTipsMsg(data);
			}
		}
		
		protected function onWordsBtnClickHandler(event:MouseEvent):void
		{
			home.back.tabMenu.arrBtns[2].dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new BasicWin();
			addChild(back);
			
			back.availUpgrade.toolTipData="提升关系";
			ToolTipsManager.register(back.availUpgrade);
			
			back.removeBtn.htmlText="".concat("<a href='event:","","'>",">>解除关系","</a>");
		}
		
		override public function destroy():void
		{
			// TODO Auto Generated method stub
			super.destroy();
		}
		
		override public function hide(event:Event=null):void
		{
			// TODO Auto Generated method stub
			super.hide(event);
		}
		
		private function initlize():void
		{
			var asso:* = home.asso;
			
			back.waterBtn.label = (main.user.sex=='female')?'求清水':'送清水';
			back.chatBtn.enabled = (asso.online !=null);
			back.sayHelloBtn.enabled = (asso.canHello=='true');
			back.idNo.text = asso.id;
			back.nikeName.text = asso.nikeName;
			
			var level:int = asso.level;
			var loc1:String = MessageUtil.decodeAssoLevel(level);
			var loc2:String = MessageUtil.decodeAssoLevel(level+1>6?6:level+1);
			back.assoDesc.assoLevel.htmlText = "当前关系：" + StringUtil.addColor(loc1,"#0099333") + "，下一层关系："+StringUtil.addColor(loc2,"#f9542b");
			back.assoDesc.gotoAndStop( int(asso.assoValue/asso.upgradeExp*back.assoDesc.totalFrames));
			
			ToolTipsManager.unregister(back.assoDesc);
			back.assoDesc.toolTipData = "关系 "+asso.assoValue+ "/" + asso.upgradeExp;
			ToolTipsManager.register(back.assoDesc);
			
			if(asso.assoValue >= asso.upgradeExp){
				back.availUpgrade.filters=[];
			}else{
				back.availUpgrade.filters=[FilterConst.colorMatrixFilterGray];
			}
			
			back.latestVisiteTime.text = asso.latestVisitAt;
			if(asso.visitDays=="0"){
				back.visiteDayLabel.text = "";
			}else{
				back.visiteDayLabel.text = "距离上次互动已过去 "+asso.visitDays+" 天";
			}
			back.dayValue.text = asso.beginDays;
			
			//back.assoValue.setValue(asso.assoValue);
			main.user.sex == 'female' ? back.userPic.gotoAndStop("male") : back.userPic.gotoAndStop("female");
			//BitmapUtil.showAvatarPic(user, back.userPic);
		}
		
		override public function show(event:Event=null):void
		{
			this.initlize();
			super.show(event);
		}
	}
}